package com.mdd.front.service.impl.yangyangLeadingEnterprise;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppUserData;
import com.mdd.common.entity.yangyangLeadingEnterprise.common.JsonReturn;
import com.mdd.common.mapper.yangyangLeadingEnterprise.TAppUserDataMapper;
import com.mdd.common.util.TimeUtils;
import com.mdd.front.service.yangyangLeadingEnterprise.ITAppUserDataService;
import com.mdd.front.validate.common.PageValidate;
import com.mdd.front.validate.yangyangLeadingEnterprise.TAppUserDataCreateValidate;
import com.mdd.front.validate.yangyangLeadingEnterprise.TAppUserDataSearchValidate;
import com.mdd.front.validate.yangyangLeadingEnterprise.TAppUserDataUpdateValidate;
import com.mdd.front.vo.yangyangLeadingEnterprise.TAppUserDataDetailVo;
import com.mdd.front.vo.yangyangLeadingEnterprise.TAppUserDataListedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 【请填写功能名称】实现类
 * @author LikeAdmin
 */
@Service
public class TAppUserDataServiceImpl implements ITAppUserDataService {

    @Resource
    TAppUserDataMapper tAppUserDataMapper;

    /**
     * 【请填写功能名称】列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TAppUserDataListedVo>
     */
    @Override
    public PageResult<TAppUserDataListedVo> list(PageValidate pageValidate, TAppUserDataSearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        QueryWrapper<TAppUserData> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        tAppUserDataMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:countryId@country_id:int",
            "like:countryName@country_name:str",
            "=:ipAdress@ip_adress:str",
            "=:longitude:long",
            "=:lalitude:long",
            "=:userId@user_id:int",
            "=:enterpriseId@enterprise_id:int",
        });

        IPage<TAppUserData> iPage = tAppUserDataMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<TAppUserDataListedVo> list = new LinkedList<>();
        for(TAppUserData item : iPage.getRecords()) {
            TAppUserDataListedVo vo = new TAppUserDataListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime().getTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime().getTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 【请填写功能名称】详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return TAppUserData
     */
    @Override
    public TAppUserDataDetailVo detail(Integer id) {
        TAppUserData model = tAppUserDataMapper.selectOne(
                new QueryWrapper<TAppUserData>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TAppUserDataDetailVo vo = new TAppUserDataDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 【请填写功能名称】新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(TAppUserDataCreateValidate createValidate) {
        TAppUserData model = new TAppUserData();
        model.setCountryId(createValidate.getCountryId());
        model.setCountryName(createValidate.getCountryName());
        model.setIpAdress(createValidate.getIpAdress());
        model.setLongitude(createValidate.getLongitude());
        model.setLalitude(createValidate.getLalitude());
        model.setUserId(createValidate.getUserId());
        model.setEnterpriseId(createValidate.getEnterpriseId());
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        tAppUserDataMapper.insert(model);
    }

    /**
     * 【请填写功能名称】编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(TAppUserDataUpdateValidate updateValidate) {
        TAppUserData model = tAppUserDataMapper.selectOne(
                new QueryWrapper<TAppUserData>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setCountryId(updateValidate.getCountryId());
        model.setCountryName(updateValidate.getCountryName());
        model.setIpAdress(updateValidate.getIpAdress());
        model.setLongitude(updateValidate.getLongitude());
        model.setLalitude(updateValidate.getLalitude());
        model.setUserId(updateValidate.getUserId());
        model.setEnterpriseId(updateValidate.getEnterpriseId());
        model.setUpdateTime(new Date());
        tAppUserDataMapper.updateById(model);
    }

    /**
     * 【请填写功能名称】删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        TAppUserData model = tAppUserDataMapper.selectOne(
                new QueryWrapper<TAppUserData>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        tAppUserDataMapper.delete(new QueryWrapper<TAppUserData>().eq("id", id));
    }

    /**
     * 用户切换企业调用接口
     * @param userDataId
     * @param enterpriseId
     * @return
     */
    @Override
    public String switchingCompanies(Integer userDataId, Integer enterpriseId) {
        if (enterpriseId == null || userDataId == null) {
            return JsonReturn.fail("必传参数不能为空",201).toJsonStr();
        }
        TAppUserData appUserData = tAppUserDataMapper.selectById(userDataId);
        if(appUserData!=null && appUserData.getId()!=null) {
            appUserData.setEnterpriseId(enterpriseId);
            tAppUserDataMapper.updateById(appUserData);
            return JsonReturn.success().toJsonStr();
        }
        return JsonReturn.fail().toJsonStr();
    }

}
